<template>

    <el-container class="container">
        
        <el-main class="main-container">
            <div v-for="blog in blogs" :key="blog.id" class="blog-item" @click="goToDetail(blog.id)">
                <h2 class="blog-title">{{ blog.title }}</h2>
                <p class="blog-content" v-html="blog.content"></p>
                <div class="blog-footer">
                    <p style="font-family: 'Times New Roman', Times, serif;">{{ blog.author_name }}</p>
                    <span class="pub-time">{{ dateformat.stringFromDateTime(blog.pub_time) }}</span>
                </div>
            </div>
        </el-main>
    </el-container>

</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import BlogHttp from '@/api/BlogHttp';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import dateformat from '@/utils/dateformat';
const router = useRouter()
let blogs = ref([])
const activeIndex = ref('0')

const handleSelect = (key) => {
    if (key === '0') {
        router.push({ name: 'home' })
    }
}
onMounted(async () => {
    try {
        let data = await BlogHttp.getBlogList()
        blogs.value = data
        console.log(data)
    } catch (e) {
        ElMessage.error('获取博客列表失败')
    }
})
const goToDetail = (blogId) => {
    router.push({
        name: "blogDetail",
        params: {
            pk: blogId
        }
    })
}
</script>
<style scoped>
.main-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
    /* 减小列表项之间的间距 */
    padding: 16px;
    margin: 0 auto;
    width: min(800px, 90%);
}

.blog-item {
    background-color: #fff;
    padding: 16px;
    border-radius: 6px;
    transition: all 0.2s ease;
    border: 1px solid #ebeef5;
    height: 150px;
    /* 添加固定高度 */
    display: flex;
    flex-direction: column;
    cursor: pointer;
}

.blog-item:hover {
    transform: translateY(-1px);
    /* 减小悬停效果 */
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* 博客标题样式 */
.blog-title {
    font-size: 0.95rem;
    /* 稍微减小标题大小 */
    font-weight: 600;
    color: #303133;
    margin: 0 0 8px;
    /* 减小下边距 */
}

/* 博客内容样式 */
.blog-content {
    flex: 1;
    font-size: 0.85rem;
    line-height: 1.4;
    color: #606266;
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    /* 减少显示行数 */
    -webkit-box-orient: vertical;
}

/* 博客底部样式 */
.blog-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 4px;
    margin-top: 4px;
    border-top: 1px solid #ebeef5;
    min-height: 24px;
    /* 设置最小高度 */
}

.pub-time {
    font-size: 0.75rem;
    /* 减小字体大小 */
    color: #909399;
}
:root.dark .blog-item {
    background-color: var(--el-bg-color);
    border-color: var(--el-border-color-darker);
}

:root.dark .blog-title {
    color: var(--el-text-color-primary);
}

:root.dark .blog-content {
    color: var(--el-text-color-regular);
}

:root.dark .pub-time {
    color: var(--el-text-color-secondary);
}

:root.dark .blog-item:hover {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
</style>